package com.pro01.apirednote.mapper;

import com.pro01.apirednote.pojo.ContentLike;
import org.apache.ibatis.annotations.*;

@Mapper
public interface ContentLikeMapper {

    @Select("SELECT * FROM content_likes WHERE user_id = #{userId} AND content_id = #{contentId}")
    ContentLike findByUserIdAndContentId(@Param("userId") Long userId, @Param("contentId") Long contentId);

    @Insert("INSERT INTO content_likes (user_id, content_id, create_time, is_deleted) " +
            "VALUES (#{userId}, #{contentId}, NOW(), false)")
    void insert(ContentLike like);

    @Update("UPDATE content_likes SET is_deleted = false, create_time = NOW() " +
            "WHERE user_id = #{userId} AND content_id = #{contentId}")
    void restore(@Param("userId") Long userId, @Param("contentId") Long contentId);

    @Update("UPDATE content_likes SET is_deleted = true " +
            "WHERE user_id = #{userId} AND content_id = #{contentId}")
    void softDelete(@Param("userId") Long userId, @Param("contentId") Long contentId);

    // 更新点赞数量
    @Update("UPDATE contents SET like_count = like_count + 1 WHERE id = #{contentId}")
    void incrementLikeCount(@Param("contentId") Long contentId);

    @Update("UPDATE contents SET like_count = like_count - 1 WHERE id = #{contentId} AND like_count > 0")
    void decrementLikeCount(@Param("contentId") Long contentId);


}